package com.springdemo.module.systemmanager.controller;

import com.springdemo.module.systemmanager.entity.UserPage;
import com.springdemo.module.systemmanager.entity.myentity.MyUser;
import com.springdemo.module.systemmanager.service.UserService;
import com.springdemo.tools.Privilege;
import com.springdemo.tools.StringHelper;
import com.springdemo.tools.jackjson.JackJson;
import com.springdemo.tools.security.AESCryptos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yiliu on 2017/8/23.
 */
@Controller
@RequestMapping("/user")
public class TestController {

    @Autowired
    private UserService<UserPage> userService;

    /**
     * 用户列表查询
     *
     * @throws Exception
     * @Title: list
     * @data: 2014-03-12 下午04:03:50
     * @author: san
     */
    @RequestMapping("/list")
    @ResponseBody
    public String list(@RequestParam("page") Integer page,@RequestParam("rows") Integer rows,@ModelAttribute("user") MyUser user) throws Exception {

        List<MyUser> dataList = userService.queryUserByList(user);


        //设置页面数据
        //总页数
        Integer total = userService.queryByCount(user) /user.getRows() +1;
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        jsonMap.put("total", total);//总页数
        jsonMap.put("rows", dataList);//list值
        jsonMap.put("records", userService.queryByCount(user));//
        jsonMap.put("page", user.getPage());
        return JackJson.fromObjectToJson(jsonMap);
    }

    /**
     * 跳转入用户列表页面
     *
     * @throws Exception
     * @Title: toUserIndex
     * @data: 2014-03-12 下午04:51:50
     * @author: shuqi
     */
    @RequestMapping("/toList")
    public String toList() throws Exception {
        return "user/userTable";

    }

    @RequestMapping("/checkEmail")
    @ResponseBody
    public String checkEmail() {
        Map<String,Boolean> result = new HashMap<>();
        result.put("valid",false);
        return JackJson.fromObjectToJson(result);
    }

    /**
     * 跳转入用户信息页面
     *
     * @throws Exception
     * @Title: toEditIndex
     * @data: 2014-03-12 下午04:51:50
     * @author: shuqi
     */
    @Privilege
    public String toEditIndex() {
        return "toEditIndex";
    }

    /**
     * 跳转入用户信息页面
     *
     * @throws Exception
     * @Title: toEditIndex
     * @data: 2014-03-12 下午04:51:50
     * @author: shuqi
     */
    @Privilege
    public String toAddIndex() {
        return "toEditIndex";
    }

    /**
     * 跳转入上传图片页面
     *
     * @throws Exception
     * @Title: touploadPic
     * @data: 2014-03-12 下午04:51:50
     * @author: shuqi
     */
    @Privilege
    public String touploadPic() {
        return "touploadPic";
    }

    /**
     * 上传正式照片
     *
     * @return
     * @Title: toRealyuploadPic
     * @data:2014-7-17下午02:25:00
     * @author:san
     */
    @Privilege
    public String toRealyuploadPic() {
        return "toRealyPic";
    }

    /**
     * 导入EXCEL模板
     *
     * @return
     */
    @Privilege
    public String importExcelPage() {
        return "toImportPage";
    }


    /**
     * 查询用户账号信息是否重复
     * @Title: noRepeat
     * @data: 2014-03-12 下午04:03:50
     * @author: shuqi
     *
     * @throws Exception
     */
    /*public void noRepeat() throws Exception{

        //加密账号
        if(!StringHelper.toTrim(getModel().getRegname()).equals("")){
            getModel().setRegname((AESCryptos.aesEncrypt(getModel().getRegname(), AESCryptos.SAVE_KEY)));
        }

        //加密手机号
        if(!StringHelper.toTrim(getModel().getCell()).equals("")){
            getModel().setCell((AESCryptos.aesEncrypt(getModel().getCell(), AESCryptos.SAVE_KEY)));
        }

		int usernum  = userService.noRepeat(getModel());

		if(usernum  == 0){
			CommonSendMeg.writeMsg("true");
			return;
		}else{
			CommonSendMeg.writeMsg("false");
			return;
		}
	}*/

    /**
     * 查询用户编号、手机号码信息是否重复
     * @Title: repeatCode
     * @data: 2014-03-12 下午04:03:50
     * @author: shuqi
     *
     * @throws Exception
     */
/*	public void repeatCode() throws Exception{
        //加密账号
        if(!StringHelper.toTrim(getModel().getRegname()).equals("")){
            getModel().setRegname((AESCryptos.aesEncrypt(getModel().getRegname(), AESCryptos.SAVE_KEY)));
        }

        //加密手机号
        if(!StringHelper.toTrim(getModel().getCell()).equals("")){
            getModel().setCell((AESCryptos.aesEncrypt(getModel().getCell(), AESCryptos.SAVE_KEY)));
        }

		int usernum  = userService.noRepeat(getModel());

		if(usernum == 0){
			CommonSendMeg.writeMsg("true");
			return;
		}else{
			CommonSendMeg.writeMsg("false");
			return;
		}
	}*/

    /**
     * 根据id查询用户信息
     *
     * @throws Exception
     * @Title: getUserById
     * @data: 2014-03-12 下午04:03:50
     * @author: shuqi
     */
    public void getUserById( @RequestParam("id") String id ) throws Exception {
        MyUser myuser = userService.queryUserById(id);
        if (myuser != null) {
            if (!StringHelper.toTrim(myuser.getRegname()).equals("")) {
                myuser.setRegname(AESCryptos.aesDecrypt(myuser.getRegname(), AESCryptos.SAVE_KEY));
            }
            if (!StringHelper.toTrim(myuser.getCell()).equals("")) {
                myuser.setCell(AESCryptos.aesDecrypt(myuser.getCell(), AESCryptos.SAVE_KEY));
            }
        }
        String msg = JackJson.fromObjectToJson(myuser);
        //CommonSendMeg.writeMsg(msg);
    }


}
